// pages/index/index.js
const app = getApp();
var modal = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    back:false,
    title:'二维码识别发票',
    billItems: [
      { pic: '/images/invoice_icon.png', text: '通用电子发票', dataType:'general'}, 
      // , type:'general'
      { pic: '/images/invoice_icon.png', text: '增值税普票', dataType: 'plain'}, 
      // , type: 'plain'
      { pic: '/images/invoice_icon.png',text: '增值税专票', type: 'special'}
    ],
    modalTitle:'退出登录',
    modalContent:'您确认要退出登录渠道发票小程序吗？',
    modalButtons:['确认','取消'],
    modalBtnFn: ['logOff','modalClose'],
    confirmHide:true,
    layerColor: 'red',
    layerPic: '/images/unhappy.png',
    layerTitle: '',
    modalHide: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    modal = require('../../utils/modal/modal.js');
    this.layerClose = modal.layerClose;
    console.log(this.layerClose)
    if (options.type){
      this.setData({
        back:true
      })
    }else{
      this.setData({
        back: false
      })
    }
    if (options.type =='special'){
      this.setData({
        billItems: [
          { pic: '/images/special_icon.png', text: '增值税专票-1%', dataType: 's1vat' },
          { pic: '/images/special_icon.png', text: '增值税专票-3%', dataType: 's3vat' },
          { pic: '/images/special_icon.png', text: '增值税专票-6%', dataType: 's6vat' }
        ]
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  toDetail(e){
    let that=this,
        index = e.currentTarget.dataset.index,
        dataType = e.currentTarget.dataset.type;
    // if (new Date().getDate() >= 21) {
    //   this.setData({
    //     modalTitle: '提示信息',
    //     modalContent: '已经超出发票可以录入时间，请于每个月21号0点之前录入',
    //     modalButtons: ['确认'],
    //     modalBtnFn: ['modalClose'],
    //     confirmHide: false
    //   })
    // } else {
      if (that.data.billItems[index].type){
        wx.navigateTo({
          url: '/pages/index/index?type=' + that.data.billItems[index].type,
        })
      }else{
        this.scanRec(dataType)
      }
    // }
  },

  scanRec(dataType){
    let that=this;
    wx.scanCode({
      success(res) {
        console.log(res)
        console.log(res.result.split(',').length,'二维码数据长度')
        function toIcvResult(){
          wx.navigateTo({
            url: '/pages/invoice/invoice?dataType=' + dataType + '&ivcParam=' + res.result,
          })
        }
        if (dataType =='general'){
          app.generalMatch(res, toIcvResult,function(msg){
            that.errLayer(msg)
          })
        } else if (dataType == 'plain'){
          app.plainMatch(res, toIcvResult, function (msg) {
            that.errLayer(msg)
          })
        } else if (dataType == 's1vat' || dataType == 's3vat' || dataType == 's6vat'){
          app.specialMatch(res, toIcvResult, function (msg) {
            that.errLayer(msg)
          })
        }
      },
      fail(res){
        wx.showToast({
          icon:'none',
          title: '扫码未成功！',
        })
      }
    })
  },

  back(){
    if(this.data.back){
      wx.navigateBack({})
    }else{
      this.setData({
        modalTitle: '退出登录',
        modalContent: '您确认要退出登录渠道发票小程序吗？',
        modalButtons: ['确认', '取消'],
        modalBtnFn: ['logOff', 'modalClose'],
        confirmHide: false
      })
    }
  },

  modalClose(){
    this.setData({
      confirmHide: true
    })
  }, 

  errLayer(msg){
    let that=this;
    this.setData({
      modalHide: false,
      layerTitle: msg
    })
    setTimeout(function () { that.layerClose() }, 1800)
  },

  logOff(){
    let that=this;
    wx.showLoading({
      title: '正在退出...',
    })
    wx.request({
      url: app.globalData.appUrl +'/auth/oauth/logout',
      // /logout
      method:'DELETE',
      header: {
        'Authorization': 'Bearer ' + app.globalData.token,
      },
      success(res){
        wx.reLaunch({
          url: '/pages/login/login',
        })
      },
      fail(res){
        that.modalClose()
        wx.hideLoading()
        that.setData({
          modalHide: false,
          layerTitle: '操作失败'
        })
        setTimeout(function () { that.layerClose() }, 1800)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareData()
  }
})